package one.stand.controller;

import com.github.pagehelper.PageHelper;
import com.jmp.api.admin.dto.CompanyLicenseRequest;
import com.jmp.api.admin.dto.CompanyLicenseResponse;
import com.jmp.autocofiguration.web.interceptor.SecurityAuthCheck;
import com.jmp.base.dto.CompanyLicenseAuditRequest;
import com.jmp.base.dto.CompanyLicenseInfoRequest;
import com.jmp.base.dto.CompanyLicenseInfoResponse;
import com.jmp.base.dto.CompanyLicenseUpdateRequest;
import com.jmp.feign.CompanyFeign;
import one.stand.dto.ConvertUtils;
import one.stand.dto.PageResponse2;
import one.stand.mapper.CompanyApiAdminMapper;
import one.stand.model.ResultModel;
import one.stand.util.AssertUtil;
import one.stand.util.SqlUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("adminApi/company/license")
public class CompanyLicenseController {
    @Autowired
    private CompanyFeign companyFeign;
    @Autowired
    private CompanyApiAdminMapper companyApiAdminMapper;

    /**
     * 申请列表
     */
    @PostMapping("list")
    @SecurityAuthCheck
    public ResultModel<CompanyLicenseResponse> list(@RequestBody CompanyLicenseRequest request) {

        String nameLike = SqlUtil.like(request.getKeyword());
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<CompanyLicenseResponse.License> list = companyApiAdminMapper.selectLicenses(nameLike, request.getStatus(), request.getType());
        PageResponse2<CompanyLicenseResponse.License> page = ConvertUtils.page2(list, license -> license);

        CompanyLicenseResponse response = CompanyLicenseResponse.builder()
                .page(page)
                .build();
        return ResultModel.success(response);
    }

    /**
     * 审核
     */
    @PostMapping("audit")
    @SecurityAuthCheck
    public ResultModel audit(@RequestBody com.jmp.api.admin.dto.CompanyLicenseAuditRequest request) {

        ResultModel resultModel = companyFeign.licenseAudit(CompanyLicenseAuditRequest.builder()
                .id(request.getId())
                .audit(request.getStatus())
                .memo(request.getMemo())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }

    /**
     * 更改类型
     */
    @PostMapping("update")
    @SecurityAuthCheck
    public ResultModel update(@RequestBody com.jmp.api.admin.dto.CompanyLicenseUpdateRequest request) {
        Integer id = request.getId();
        if (Objects.nonNull(request.getCompanyId())) {
            ResultModel<CompanyLicenseInfoResponse> licenseInfoResponseResultModel = companyFeign.licenseInfo(CompanyLicenseInfoRequest.builder()
                    .companyId(request.getCompanyId())
                    .build());
            AssertUtil.isTrue(licenseInfoResponseResultModel);
            id = licenseInfoResponseResultModel.getData().getId();
        }


        ResultModel resultModel = companyFeign.licenseUpdate(CompanyLicenseUpdateRequest.builder()
                .id(id)
                .companyId(request.getCompanyId())
                .type(request.getType())
                .licensePhotos(request.getLicensePhotos())
                .build());
        AssertUtil.isTrue(resultModel);

        return ResultModel.success();
    }
}
